﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for Services
/// </summary>
public class Services
{

    #region GetDonVi_NghanhResult
    public static List<GetDonVi_NghanhResult> GetDonVi_Nghanh()
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetDonVi_Nghanh();
                List<GetDonVi_NghanhResult> lst = new List<GetDonVi_NghanhResult>();
                GetDonVi_NghanhResult entity;
                foreach (GetDonVi_NghanhResult item in lstReportIP)
                {
                    entity = new GetDonVi_NghanhResult();
                    entity.DonViID = item.DonViID;
                    entity.NganhID = item.NganhID;
                    entity.TEN_NGANH = item.TEN_NGANH;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetDonVi_NghanhResult>();
            }
        }
    }
    #endregion

    #region GetF810Result
    public static List<GetF810Result> GetF810(int ip_int_donviId, int ip_int_nganhId)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetF810(ip_int_donviId,ip_int_nganhId);
                List<GetF810Result> lst = new List<GetF810Result>();
                GetF810Result entity;
                foreach (GetF810Result item in lstReportIP)
                {
                    entity = new GetF810Result();
                    entity.RowNumber = item.RowNumber;
                    entity.TEN_NGANH = item.TEN_NGANH;
                    entity.DA_XUAT_BAN_YN = item.DA_XUAT_BAN_YN;
                    entity.GHI_CHU = item.GHI_CHU;
                    entity.MA_MON_HOC = item.MA_MON_HOC;
                    entity.NGAY_CAP = item.NGAY_CAP;
                    entity.NGAY_VERSION = item.NGAY_VERSION;
                    entity.TEN_SAN_PHAM = item.TEN_SAN_PHAM;
                    entity.TEN_TIENG_VIET = item.TEN_TIENG_VIET;
                    entity.TEN_VERSION = item.TEN_VERSION;
                   
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetF810Result>();
            }
        }
    }
    #endregion

    #region GetF820Result
    public static List<GetF820Result> GetF820(int ip_int_monhocId, int ip_int_sanphamId, int ip_int_phienbanId, string ip_str_trangthai)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetF820(ip_int_monhocId, ip_int_sanphamId, ip_int_phienbanId, ip_str_trangthai);
                List<GetF820Result> lst = new List<GetF820Result>();
                GetF820Result entity;
                foreach (GetF820Result item in lstReportIP)
                {
                    entity = new GetF820Result();
                    entity.RowNumber = item.RowNumber;
                    entity.DA_XUAT_BAN_YN = item.DA_XUAT_BAN_YN;
                    entity.TEN_DON_VI = item.TEN_DON_VI;
                    entity.TEN_NGANH = item.TEN_NGANH;
                    entity.NGAY_VERSION = item.NGAY_VERSION;
                    entity.TEN_TIENG_VIET = item.TEN_TIENG_VIET;
                    entity.TEN_VERSION = item.TEN_TIENG_VIET;

                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetF820Result>();
            }
        }
    }
    #endregion

    #region GetF830Result
    public static List<GetF830Result> GetF830(int ip_int_donviId, int ip_int_nganhId)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetF830(ip_int_donviId, ip_int_nganhId);
                List<GetF830Result> lst = new List<GetF830Result>();
                GetF830Result entity;
                foreach (GetF830Result item in lstReportIP)
                {
                    entity = new GetF830Result();
                    entity.RowNumber = item.RowNumber;
                    entity.TEN_NGANH = item.TEN_NGANH;                    
                    entity.MA_MON_HOC = item.MA_MON_HOC;
                    entity.TEN_DON_VI = item.TEN_DON_VI;
                    entity.MA_MON_HOC_MOI = item.MA_MON_HOC_MOI;
                    entity.TEN_TIENG_VIET = item.TEN_TIENG_VIET;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetF830Result>();
            }
        }
    }

    #endregion
    #region GetcboNganhByDonViIdResult
    public static List<GetcboNganhByDonViIdResult> GetcboNganhByDonViId(int ip_int_don_vi_id)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetcboNganhByDonViId(ip_int_don_vi_id);
                List<GetcboNganhByDonViIdResult> lst = new List<GetcboNganhByDonViIdResult>();
                GetcboNganhByDonViIdResult entity;
                foreach (GetcboNganhByDonViIdResult item in lstReportIP)
                {
                    entity = new GetcboNganhByDonViIdResult();
                    entity.ID = item.ID;
                    entity.TEN_NGANH = item.TEN_NGANH;
                    entity.MA_NGANH = item.MA_NGANH;
                    entity.GHI_CHU = item.GHI_CHU;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetcboNganhByDonViIdResult>();
            }
        }
    }
    #endregion

    #region GetcboMonHocByDonViNganhIdResult
    public static List<GetcboMonHocByDonViNganhIdResult> GetcboMonHocByDonViNganhId(int ip_int_don_vi_id, int ip_int_nganh_id)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetcboMonHocByDonViNganhId(ip_int_don_vi_id, ip_int_nganh_id);
                List<GetcboMonHocByDonViNganhIdResult> lst = new List<GetcboMonHocByDonViNganhIdResult>();
                GetcboMonHocByDonViNganhIdResult entity;
                foreach (GetcboMonHocByDonViNganhIdResult item in lstReportIP)
                {
                    entity = new GetcboMonHocByDonViNganhIdResult();
                    entity.ID = item.ID;
                    entity.MA_MON_HOC = item.MA_MON_HOC;
                    entity.TEN_TIENG_ANH = item.TEN_TIENG_ANH;
                    entity.TEN_TIENG_VIET = item.TEN_TIENG_VIET;
                    entity.GHI_CHU = item.GHI_CHU;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetcboMonHocByDonViNganhIdResult>();
            }
        }
    }
    #endregion

    #region GetcbosanPhamByMonHocIdResult
    public static List<GetcbosanPhamByMonHocIdResult> GetcbosanPhamByMonHocId(int ip_int_mon_hoc_id)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetcbosanPhamByMonHocId(ip_int_mon_hoc_id);
                List<GetcbosanPhamByMonHocIdResult> lst = new List<GetcbosanPhamByMonHocIdResult>();
                GetcbosanPhamByMonHocIdResult entity;
                foreach (GetcbosanPhamByMonHocIdResult item in lstReportIP)
                {
                    entity = new GetcbosanPhamByMonHocIdResult();
                    entity.ID = item.ID;
                    entity.MA_SAN_PHAM = item.MA_SAN_PHAM;
                    entity.TEN_SAN_PHAM = item.TEN_SAN_PHAM;
                    entity.MA_NHOM_SAN_PHAM = item.MA_NHOM_SAN_PHAM;
                    entity.GHI_CHU = item.GHI_CHU;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetcbosanPhamByMonHocIdResult>();
            }
        }
    }
    #endregion

    #region GetMonHocResult
    public static List<GetMonHocResult> GetMonHoc(int donviId, int nghanhId)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetMonHoc(donviId, nghanhId);
                List<GetMonHocResult> lst = new List<GetMonHocResult>();
                GetMonHocResult entity;
                foreach (GetMonHocResult item in lstReportIP)
                {
                    entity = new GetMonHocResult();
                    entity.ID = item.ID;
                    entity.MA_MON_HOC_MOI = item.MA_MON_HOC_MOI;
                    entity.MA_MON_HOC = item.MA_MON_HOC;
                    entity.TEN_TIENG_ANH = item.TEN_TIENG_ANH;
                    entity.TEN_TIENG_VIET = item.TEN_TIENG_VIET;
                    entity.GHI_CHU = item.GHI_CHU;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetMonHocResult>();
            }
        }
    }
    #endregion

    #region GetPhienBanByMonHocSanPhamResult
    public static List<GetPhienBanByMonHocSanPhamResult> GetPhienBanByMonHocSanPham(int ip_int_monhocid, int ip_int_sanphamid)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetPhienBanByMonHocSanPham(ip_int_monhocid, ip_int_sanphamid);
                List<GetPhienBanByMonHocSanPhamResult> lst = new List<GetPhienBanByMonHocSanPhamResult>();
                GetPhienBanByMonHocSanPhamResult entity;
                foreach (GetPhienBanByMonHocSanPhamResult item in lstReportIP)
                {
                    entity = new GetPhienBanByMonHocSanPhamResult();
                    entity.ID = item.ID;
                    entity.MA_VERSION = item.MA_VERSION;
                    entity.TEN_VERSION = item.TEN_VERSION;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetPhienBanByMonHocSanPhamResult>();
            }
        }
    }
    #endregion

    #region GetSanPhamByMonHocResult
    public static List<GetSanPhamByMonHocResult> GetSanPhamByMonHoc(int ip_int_monhocid)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetSanPhamByMonHoc(ip_int_monhocid);
                List<GetSanPhamByMonHocResult> lst = new List<GetSanPhamByMonHocResult>();
                GetSanPhamByMonHocResult entity;
                foreach (GetSanPhamByMonHocResult item in lstReportIP)
                {
                    entity = new GetSanPhamByMonHocResult();
                    entity.ID = item.ID;
                    entity.MA_SAN_PHAM = item.MA_SAN_PHAM;
                    entity.TEN_SAN_PHAM = item.TEN_SAN_PHAM;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetSanPhamByMonHocResult>();
            }
        }
    }
    #endregion

    #region GetSanPhamByMonHocForGridResult
    public static List<GetSanPhamByMonHocForGridResult> GetSanPhamByMonHocForGrid(int ip_int_monhocid)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetSanPhamByMonHocForGrid(ip_int_monhocid);
                List<GetSanPhamByMonHocForGridResult> lst = new List<GetSanPhamByMonHocForGridResult>();
                GetSanPhamByMonHocForGridResult entity;
                foreach (GetSanPhamByMonHocForGridResult item in lstReportIP)
                {
                    entity = new GetSanPhamByMonHocForGridResult();
                    entity.ID = item.ID;
                    entity.MA_SAN_PHAM = item.MA_SAN_PHAM;
                    entity.TEN_SAN_PHAM = item.TEN_SAN_PHAM;
                    entity.MA_NHOM_SAN_PHAM = item.MA_NHOM_SAN_PHAM;
                    entity.GHI_CHU = item.GHI_CHU;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetSanPhamByMonHocForGridResult>();
            }
        }
    }
    #endregion

    #region GetGetPhienBanByMonHocSanPhamForGridResult
    public static List<GetPhienBanByMonHocSanPhamForGridResult> GetPhienBanByMonHocSanPhamForGrid(int ip_int_monhocid, int ip_int_sanpahmId)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetPhienBanByMonHocSanPhamForGrid(ip_int_monhocid,ip_int_sanpahmId);
                List<GetPhienBanByMonHocSanPhamForGridResult> lst = new List<GetPhienBanByMonHocSanPhamForGridResult>();
                GetPhienBanByMonHocSanPhamForGridResult entity;
                foreach (GetPhienBanByMonHocSanPhamForGridResult item in lstReportIP)
                {
                    entity = new GetPhienBanByMonHocSanPhamForGridResult();
                    entity.ID = item.ID;
                    entity.DA_XUAT_BAN_YN = item.DA_XUAT_BAN_YN;
                    entity.ID_MON_HOC_SAN_PHAN = item.ID_MON_HOC_SAN_PHAN;
                    entity.MA_VERSION = item.MA_VERSION;
                    entity.NGAY_VERSION = item.NGAY_VERSION;
                    entity.TEN_VERSION = item.TEN_VERSION;
                    entity.GHI_CHU = item.GHI_CHU;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetPhienBanByMonHocSanPhamForGridResult>();
            }
        }
    }
    #endregion

    #region GetSanPhamDonViResult
    public static List<GetSanPhamDonViResult> GetGDPhienBan(int donviId, int nghanhId, int monhocId)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var lstReportIP = context.GetSanPhamDonVi(donviId, nghanhId, monhocId);
                List<GetSanPhamDonViResult> lst = new List<GetSanPhamDonViResult>();
                GetSanPhamDonViResult entity;
                foreach (GetSanPhamDonViResult item in lstReportIP)
                {
                    entity = new GetSanPhamDonViResult();
                    entity.GHI_CHU = item.GHI_CHU;
                    entity.MA_SAN_PHAM = item.MA_SAN_PHAM;
                    entity.NGAY_CAP = item.NGAY_CAP;
                    entity.TEN_SAN_PHAM = item.TEN_SAN_PHAM;
                    entity.TEN_VERSION = item.TEN_VERSION;
                    entity.ID = item.ID;
                    lst.Add(entity);
                }
                return lst;
            }
            catch (Exception)
            {
                return new List<GetSanPhamDonViResult>();
            }
        }
    }
    #endregion

    #region DM_DON_VI_SU_DUNG
    #region GetDsDM_DON_VI_SU_DUNG
    public static List<DM_DON_VI_SU_DUNG> GetDsDM_DON_VI_SU_DUNG()
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_DON_VI_SU_DUNGs.ToList<DM_DON_VI_SU_DUNG>();
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new List<DM_DON_VI_SU_DUNG>();
        }
    }
    #endregion

    #region GetDM_DON_VI_SU_DUNGById
    public static DM_DON_VI_SU_DUNG GetDM_DON_VI_SU_DUNGById(int id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_DON_VI_SU_DUNGs.Single(b => (int)b.ID == id);
            }
        }
        catch (Exception)
        {
            return new DM_DON_VI_SU_DUNG();
        }
    }
    #endregion

    #region GetDM_DON_VI_NGANH_MON_HOCById
    public static DM_DON_VI_NGANH_MON_HOC GetDM_DON_VI_NGANH_MON_HOCById(int id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_DON_VI_NGANH_MON_HOCs.Single(b => (int)b.ID == id);
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new DM_DON_VI_NGANH_MON_HOC();
        }
    }
    #endregion

    #region InsertDM_DON_VI_SU_DUNG
    public static bool InsertDM_DON_VI_SU_DUNG(DM_DON_VI_SU_DUNG ip_obj_DM_DON_VI_SU_DUNG)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.DM_DON_VI_SU_DUNGs.InsertOnSubmit(ip_obj_DM_DON_VI_SU_DUNG);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region UpdateDM_DON_VI_SU_DUNG
    public static bool UpdateDM_DON_VI_SU_DUNG(DM_DON_VI_SU_DUNG ip_obj_DM_DON_VI_SU_DUNG)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.DM_DON_VI_SU_DUNGs.Single(b => b.ID == ip_obj_DM_DON_VI_SU_DUNG.ID);
            query.MA_DIA_DIEM = ip_obj_DM_DON_VI_SU_DUNG.MA_DIA_DIEM;
            query.MA_DON_VI = ip_obj_DM_DON_VI_SU_DUNG.MA_DON_VI;
            query.TEN_DON_VI = ip_obj_DM_DON_VI_SU_DUNG.TEN_DON_VI;
            query.DIA_CHI = ip_obj_DM_DON_VI_SU_DUNG.DIA_CHI;
            query.DIEN_THOAI = ip_obj_DM_DON_VI_SU_DUNG.DIEN_THOAI;
            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DeleteDM_DON_VI_SU_DUNG
    public static bool DeleteDM_DON_VI_SU_DUNG(int id)
    {
        bool ret = true;

        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                context.DeleteDM_DON_VI_SU_DUNG(id);
                ret = true;
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #endregion

    #region DM_NGANH_DAO_TAO
    #region GetDM_NGANH_DAO_TAOById
    public static DM_NGANH_DAO_TAO GetDM_NGANH_DAO_TAOById(int id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_NGANH_DAO_TAOs.Single(b => (int)b.ID == id);
            }
        }
        catch (Exception)
        {
            return new DM_NGANH_DAO_TAO();
        }
    }
    #endregion

    #region GetDsDM_NGANH_DAO_TAO
    public static List<DM_NGANH_DAO_TAO> GetDsDM_NGANH_DAO_TAO()
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                var nganh = from n in context.DM_NGANH_DAO_TAOs
                            select n;
                return nganh.ToList<DM_NGANH_DAO_TAO>();
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new List<DM_NGANH_DAO_TAO>();
        }
    }
    #endregion

    #region GetDsDM_NGANH_DAO_TAO
    public static List<DM_NGANH_DAO_TAO> GetDsDM_NGANH_DAO_TAOByDonViId(int ip_int_DonViId)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                var nganh = from n in context.DM_NGANH_DAO_TAOs
                            join d in context.DM_DON_VI_NGANHs
                            on n.ID equals d.ID_NGANH
                            where d.ID_DON_VI_SU_DUNG == ip_int_DonViId
                            select n;
                return nganh.ToList<DM_NGANH_DAO_TAO>();
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new List<DM_NGANH_DAO_TAO>();
        }
    }
    #endregion

    #region GetDsDM_NGANH_DAO_TAO
    public static List<DM_NGANH_DAO_TAO> GetDsDM_NGANH_DAO_TAOByDVId(int ip_int_DonViId)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                var nganh = from n in context.DM_NGANH_DAO_TAOs
                            join d in context.DM_DON_VI_NGANHs
                            on n.ID equals d.ID_NGANH
                            where d.ID_DON_VI_SU_DUNG == ip_int_DonViId
                            select n;
                return nganh.ToList();
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new List<DM_NGANH_DAO_TAO>();
        }        
    }
    #endregion

    #region InsertDM_NGANH_DAO_TAO
    public static bool InsertDM_NGANH_DAO_TAO(DM_NGANH_DAO_TAO ip_obj_DM_NGANH_DAO_TAO)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.DM_NGANH_DAO_TAOs.InsertOnSubmit(ip_obj_DM_NGANH_DAO_TAO);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region UpdateDM_NGANH_DAO_TAO
    public static bool UpdateDM_NGANH_DAO_TAO(DM_NGANH_DAO_TAO ip_obj_DM_NGANH_DAO_TAO)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.DM_NGANH_DAO_TAOs.Single(b => b.ID == ip_obj_DM_NGANH_DAO_TAO.ID);
            query.MA_NGANH = ip_obj_DM_NGANH_DAO_TAO.MA_NGANH;
            query.TEN_NGANH = ip_obj_DM_NGANH_DAO_TAO.TEN_NGANH;
            query.GHI_CHU = ip_obj_DM_NGANH_DAO_TAO.GHI_CHU;

            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DeleteDM_NGANH_DAO_TAO
    public static bool DeleteDM_NGANH_DAO_TAO(int id)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                context.DeleteDM_NGANH_DAO_TAO(id);
                ret = true;
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                ret = false;
            }
        }
        return ret;
    }
    #endregion

    #endregion

    #region GetDM_DON_VI_NGANHByDonViIdNganhId
    public static DM_DON_VI_NGANH GetDM_DON_VI_NGANHByDonViIdNganhId(int ip_don_vi_id, int ip_nganh_id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_DON_VI_NGANHs.SingleOrDefault(b => (int)b.ID_DON_VI_SU_DUNG == ip_don_vi_id && b.ID_NGANH == ip_nganh_id);
            }
        }
        catch (Exception)
        {
            return new DM_DON_VI_NGANH();
        }
    }
    #endregion

    #region InsertDM_DON_VI_NGANH
    public static bool InsertDM_DON_VI_NGANH(DM_DON_VI_NGANH ip_obj_DM_DON_VI_NGANH)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.DM_DON_VI_NGANHs.InsertOnSubmit(ip_obj_DM_DON_VI_NGANH);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region DeleteDM_DON_VI_NGANH
    public static bool DeleteDM_DON_VI_NGANH(int idNganh)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                context.DeleteDM_DON_VI_NGANH(idNganh);
                ret = true;
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                ret = false;
            }
        }
        return ret;
    }
    #endregion

    #region DM_DON_VI_NGANH_MON_HOC
    #region GetDM_DON_VI_NGANH(int ip_int_donvi_id, int ip_int_nghanh_id, int ip_int_monhoc_id)
    public static DM_DON_VI_NGANH_MON_HOC GetDM_DON_VI_NGANH_MON_HOC(int ip_int_donvi_id, int ip_int_nghanh_id, int ip_int_monhoc_id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                var v_obj_dm_don_vi_nganh =
           from p in context.DM_DON_VI_NGANH_MON_HOCs
           join n in context.DM_DON_VI_NGANHs
           on p.ID_DON_VI_NGANH equals n.ID
           where p.ID_MON_HOC == ip_int_monhoc_id
           && n.ID_DON_VI_SU_DUNG == ip_int_donvi_id
           && n.ID_NGANH == ip_int_nghanh_id
           select p;
                return v_obj_dm_don_vi_nganh.SingleOrDefault<DM_DON_VI_NGANH_MON_HOC>();
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new DM_DON_VI_NGANH_MON_HOC();
        }
    }

    #endregion
    #region InsertDM_DON_VI_NGANH_MON_HOC
    public static bool InsertDM_DON_VI_NGANH_MON_HOC(DM_DON_VI_NGANH_MON_HOC ip_obj_DM_DON_VI_NGANH_MON_HOC)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.DM_DON_VI_NGANH_MON_HOCs.InsertOnSubmit(ip_obj_DM_DON_VI_NGANH_MON_HOC);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region UpdateDM_DON_VI_NGANH_MON_HOC
    public static bool UpdateDM_DON_VI_NGANH_MON_HOC(DM_DON_VI_NGANH_MON_HOC ip_obj_DM_DON_VI_NGANH_MON_HOC)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.DM_DON_VI_NGANH_MON_HOCs.Single(b => b.ID == ip_obj_DM_DON_VI_NGANH_MON_HOC.ID);
            query.ID_MON_HOC = ip_obj_DM_DON_VI_NGANH_MON_HOC.ID_MON_HOC;
            query.MA_MON_HOC_MOI = ip_obj_DM_DON_VI_NGANH_MON_HOC.MA_MON_HOC_MOI;
            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DeleteDM_DON_VI_NGANH_MON_HOC
    public static bool DeleteDM_DON_VI_NGANH_MON_HOC(int id)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                context.DeleteDM_DON_VI_NGANH_MON_HOC(id);
                ret = true;
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                ret = false;
            }
        }
        return ret;
    }
    #endregion

    #endregion

    #region DM_MON_HOC
    #region GetDM_MON_HOCById
    public static DM_MON_HOC GetDM_MON_HOCById(int id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_MON_HOCs.Single(b => (int)b.ID == id);
            }
        }
        catch (Exception)
        {
            return new DM_MON_HOC();
        }
    }
    #endregion

    #region GetDsDM_MON_HOC
    public static List<DM_MON_HOC> GetDsDM_MON_HOC()
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_MON_HOCs.ToList<DM_MON_HOC>();
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new List<DM_MON_HOC>();
        }
    }
    #endregion

    #region GetDsDM_MON_HOC
    public static List<DM_MON_HOC> GetDsDM_MON_HOC(int ip_int_donvi_id, int ip_int_nghanh_id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                var v_obj_mon_hoc =
           from mh in context.DM_MON_HOCs
           join dnm in context.DM_DON_VI_NGANH_MON_HOCs
           on mh.ID equals dnm.ID_MON_HOC
           join dvn in context.DM_DON_VI_NGANHs
           on dnm.ID_DON_VI_NGANH equals dvn.ID
           where dvn.ID_DON_VI_SU_DUNG == ip_int_donvi_id
           && dvn.ID_NGANH == ip_int_nghanh_id
           select mh;
                return v_obj_mon_hoc.ToList<DM_MON_HOC>();

            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new List<DM_MON_HOC>();
        }
    }
    #endregion

    #region InsertDM_MON_HOC
    public static bool InsertDM_MON_HOC(DM_MON_HOC ip_obj_DM_MON_HOC)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.DM_MON_HOCs.InsertOnSubmit(ip_obj_DM_MON_HOC);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region UpdateDM_MON_HOC
    public static bool UpdateDM_MON_HOC(DM_MON_HOC ip_obj_DM_MON_HOC)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.DM_MON_HOCs.Single(b => b.ID == ip_obj_DM_MON_HOC.ID);
            query.MA_MON_HOC = ip_obj_DM_MON_HOC.MA_MON_HOC;
            query.TEN_TIENG_VIET = ip_obj_DM_MON_HOC.TEN_TIENG_VIET;
            query.TEN_TIENG_ANH = ip_obj_DM_MON_HOC.TEN_TIENG_ANH;
            query.GHI_CHU = ip_obj_DM_MON_HOC.GHI_CHU;

            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DeleteDM_MON_HOC
    public static bool DeleteDM_MON_HOC(int id)
    {
        bool ret = true;

        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {            
            try
            {
                context.DeleteDM_MON_HOC(id);
                ret = true;
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }

    #endregion
    #endregion

    #region GetDM_DON_VI_NGANH(int ip_int_donvi_id, int ip_int_nghanh_id, int ip_int_monhoc_id)
    public static DM_DON_VI_NGANH GetDM_DON_VI_NGANH(int ip_int_donvi_id, int ip_int_nghanh_id, int ip_int_monhoc_id)
    {
        return new DM_DON_VI_NGANH();
        //try
        //{
        //    using (DatabaseDataContext context = DataFactory.GetDataContext())
        //    {
        //        var v_obj_dm_don_vi_nganh =
        //   from p in context.DM_DON_VI_NGANHs
        //   where p.ID_MON_HOC == ip_int_monhoc_id
        //   && p.ID_DON_VI_SU_DUNG == ip_int_donvi_id
        //   && p.ID_NGANH == ip_int_nghanh_id
        //   select p;
        //        return v_obj_dm_don_vi_nganh.Single<DM_DON_VI_NGANH>();
        //    }
        //}
        //catch (Exception)
        //{
        //    return new DM_DON_VI_NGANH();
        //}
    }
    #endregion

    #region GetGD_SAN_PHAM_DON_VIById
    public static GD_SAN_PHAM_DON_VI GetDetailGDSanPham(int id)
    {
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var query = from bn in context.GD_SAN_PHAM_DON_VIs
                            where bn.ID == id
                            select bn;

                return query.Single<GD_SAN_PHAM_DON_VI>();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                return null;
            }
        }
    }
    #endregion

    #region InsertGD_SAN_PHAM_DON_VI
    public static bool InsertGDSanPham(GD_SAN_PHAM_DON_VI ip_obj_gd_san_pham)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.GD_SAN_PHAM_DON_VIs.InsertOnSubmit(ip_obj_gd_san_pham);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region UpdateGD_SAN_PHAM_DON_VI
    public static bool UpdateGDSanPham(GD_SAN_PHAM_DON_VI ip_obj_gd_san_pham)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.GD_SAN_PHAM_DON_VIs.Single(b => b.ID == ip_obj_gd_san_pham.ID);
            query.GHI_CHU = ip_obj_gd_san_pham.GHI_CHU;
            //query.ID_DON_VI_NGANH = ip_obj_gd_san_pham.ID_DON_VI_NGANH;
            query.ID_MON_HOC = ip_obj_gd_san_pham.ID_MON_HOC;
            query.ID_SAN_PHAM = ip_obj_gd_san_pham.ID_SAN_PHAM;
            query.ID_VERSION = ip_obj_gd_san_pham.ID_VERSION;
            query.NGAY_CAP = ip_obj_gd_san_pham.NGAY_CAP;
            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DeleteGD_SAN_PHAM_DON_VI
    public static bool DeleteGDSanPham(int id)
    {
        bool ret = true;

        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.GD_SAN_PHAM_DON_VIs.Single(bn => bn.ID == id);

            context.GD_SAN_PHAM_DON_VIs.DeleteOnSubmit(query);

            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DM_SAN_PHAM
    #region GetDsDM_SAN_PHAM
    public static List<DM_SAN_PHAM> GetDsDM_SAN_PHAM()
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_SAN_PHAMs.ToList<DM_SAN_PHAM>();
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new List<DM_SAN_PHAM>();
        }
    }
    #endregion

    #region GetDsDM_SAN_PHAM
    public static List<DM_SAN_PHAM> GetDsDM_SAN_PHAMBy(int ip_int_monhocId)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                var v_obj_mon_hoc =
                       from s in context.DM_SAN_PHAMs
                       join m in context.MON_HOC_SAN_PHAMs
                       on s.ID equals m.ID_SAN_PHAM
                       where m.ID_MON_HOC == ip_int_monhocId
                   select s;
                   return v_obj_mon_hoc.ToList<DM_SAN_PHAM>();
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new List<DM_SAN_PHAM>();
        }
    }

    #endregion

    #region GetDM_SAN_PHAM
    public static DM_SAN_PHAM GetDM_SAN_PHAM(int id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_SAN_PHAMs.Single(b => (int)b.ID == id);
            }
        }
        catch (Exception)
        {
            return new DM_SAN_PHAM();
        }
    }
    #endregion


    #region InsertDM_SAN_PHAM
    public static bool InsertDM_SAN_PHAM(DM_SAN_PHAM ip_obj_DM_SAN_PHAM)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.DM_SAN_PHAMs.InsertOnSubmit(ip_obj_DM_SAN_PHAM);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region UpdateDM_SAN_PHAM
    public static bool UpdateDM_SAN_PHAM(DM_SAN_PHAM ip_obj_DM_SAN_PHAM)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.DM_SAN_PHAMs.Single(b => b.ID == ip_obj_DM_SAN_PHAM.ID);
            query.GHI_CHU = ip_obj_DM_SAN_PHAM.GHI_CHU;
            query.MA_NHOM_SAN_PHAM = ip_obj_DM_SAN_PHAM.MA_NHOM_SAN_PHAM;
            query.MA_SAN_PHAM = ip_obj_DM_SAN_PHAM.MA_SAN_PHAM;
            query.TEN_SAN_PHAM = ip_obj_DM_SAN_PHAM.TEN_SAN_PHAM;

            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DeleteDM_SAN_PHAM
    public static bool DeleteDM_SAN_PHAM(int id)
    {
        bool ret = true;

        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                context.DeleteDM_SAN_PHAM(id);
                ret = true;
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #endregion

    #region MON_HOC_SAN_PHAM
    #region GetMON_HOC_SAN_PHAM
    public static MON_HOC_SAN_PHAM GetMON_HOC_SAN_PHAM(int id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.MON_HOC_SAN_PHAMs.Single(b => (int)b.ID == id);
            }
        }
        catch (Exception)
        {
            return new MON_HOC_SAN_PHAM();
        }
    }
    #endregion   

    #region GetMON_HOC_SAN_PHAM
    public static MON_HOC_SAN_PHAM GetMON_HOC_SAN_PHAM(int ip_int_monhocId, int ip_int_sanphamId)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.MON_HOC_SAN_PHAMs.SingleOrDefault(b => (int)b.ID_MON_HOC == ip_int_monhocId && (int)b.ID_SAN_PHAM == ip_int_sanphamId );
            }
        }
        catch (Exception)
        {
            return new MON_HOC_SAN_PHAM();
        }
    }
    #endregion

    #region InsertMON_HOC_SAN_PHAM
    public static bool InsertMON_HOC_SAN_PHAM(MON_HOC_SAN_PHAM ip_obj_MON_HOC_SAN_PHAM)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.MON_HOC_SAN_PHAMs.InsertOnSubmit(ip_obj_MON_HOC_SAN_PHAM);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region UpdateMON_HOC_SAN_PHAM
    public static bool UpdateMON_HOC_SAN_PHAM(MON_HOC_SAN_PHAM ip_obj_MON_HOC_SAN_PHAM)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.MON_HOC_SAN_PHAMs.Single(b => b.ID == ip_obj_MON_HOC_SAN_PHAM.ID);
            query.GHI_CHU = ip_obj_MON_HOC_SAN_PHAM.GHI_CHU;
            query.ID_MON_HOC = ip_obj_MON_HOC_SAN_PHAM.ID_MON_HOC;
            query.ID_SAN_PHAM = ip_obj_MON_HOC_SAN_PHAM.ID_SAN_PHAM;
            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DeleteMON_HOC_SAN_PHAM
    public static bool DeleteMON_HOC_SAN_PHAM(int id)
    {
        bool ret = true;

        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                context.DeleteMON_HOC_SAN_PHAM(id);
                ret = true;
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion
    #endregion

    #region DM_SAN_PHAM_VERSION   

    #region GetDM_SAN_PHAM_VERSION
    public static DM_SAN_PHAM_VERSION GetDM_SAN_PHAM_VERSION(int id)
    {
        try
        {
            using (DatabaseDataContext context = DataFactory.GetDataContext())
            {
                return context.DM_SAN_PHAM_VERSIONs.Single(b => (int)b.ID == id);
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
            return new DM_SAN_PHAM_VERSION();
        }
    }
    #endregion


    #region InsertDM_SAN_PHAM_VERSION
    public static bool InsertDM_SAN_PHAM_VERSION(DM_SAN_PHAM_VERSION ip_obj_DM_SAN_PHAM_VERSION)
    {
        bool v_bol_ret = true;
        using (DatabaseDataContext v_ds_context = DataFactory.GetDataContext())
        {
            try
            {
                v_ds_context.DM_SAN_PHAM_VERSIONs.InsertOnSubmit(ip_obj_DM_SAN_PHAM_VERSION);
                v_ds_context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                v_bol_ret = false;
            }
        }

        return v_bol_ret;
    }
    #endregion

    #region UpdateDM_SAN_PHAM_VERSION
    public static bool UpdateDM_SAN_PHAM_VERSION(DM_SAN_PHAM_VERSION ip_obj_DM_SAN_PHAM_VERSION)
    {
        bool ret = true;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            var query = context.DM_SAN_PHAM_VERSIONs.Single(b => b.ID == ip_obj_DM_SAN_PHAM_VERSION.ID);
            query.GHI_CHU = ip_obj_DM_SAN_PHAM_VERSION.GHI_CHU;
            query.DA_XUAT_BAN_YN = ip_obj_DM_SAN_PHAM_VERSION.DA_XUAT_BAN_YN;
            query.ID = ip_obj_DM_SAN_PHAM_VERSION.ID;
            query.ID_MON_HOC_SAN_PHAN = ip_obj_DM_SAN_PHAM_VERSION.ID_MON_HOC_SAN_PHAN;
            query.MA_VERSION = ip_obj_DM_SAN_PHAM_VERSION.MA_VERSION;
            query.NGAY_VERSION = ip_obj_DM_SAN_PHAM_VERSION.NGAY_VERSION;
            query.TEN_VERSION = ip_obj_DM_SAN_PHAM_VERSION.TEN_VERSION;
            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #region DeleteDM_SAN_PHAM_VERSION
    public static bool DeleteDM_SAN_PHAM_VERSION(int id)
    {
        bool ret = true;

        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                context.DeleteDM_SAN_PHAM_VERSION(id);
                ret = true;
            }
            catch (Exception ex)
            {
                // Write log
                Logger.Instance.Write(ex);
                ret = false;
            }
        }

        return ret;
    }
    #endregion

    #endregion

    public class CBOEntity
    {
        public int Value { get; set; }
        public string Text { get; set; }
    }

    
}